package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.dao.AppLoginLogDao;
import net.huashitong.supervision.task.entity.AppLoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ling
 * @date: 2020/2/4
 * @time: 11:07
 * @version: 1.0
 * @Description:
 */
@Service
public class
AppLoginLogService extends StringPKService<AppLoginLog> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private AppLoginLogDao dao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysDeptService sysDeptService;
    @Override
    protected BaseDao<AppLoginLog, String> getDao() {
        return dao;
    }
    @Override
    protected void beforeSave(AppLoginLog obj) {
        obj.setSysId(tableService.updateMaxSysId("t_app_login_log",null));
    }

    /**
     * app登录日志列表
     * @param loginLog
     * @return
     */
    public List<Map<String,Object>> queryList(AppLoginLog loginLog){
//        List<AppLoginLog> appLoginLogs = dao.queryList(loginLog);
        Map<String, Object> map =null;
        List<Map<String,Object>> mapList= new ArrayList<>();
        List<SysLogin> sysLogins = sysLoginService.queryList();//当前所有用户
        for (SysLogin sysLogin : sysLogins) {
            map= new HashMap<>();
            //当前用户条件下的登录记录
            List<AppLoginLog> loginLogs= dao.getByUserId(sysLogin.getSysId(),loginLog.getCreateDate());
            SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
            map.put("userId",sysLogin.getSysId());
            map.put("userName",sysLogin.getUsername());
            map.put("deptId",sysLogin.getDeptId());
            map.put("deptName",dept.getDeptName());
            map.put("roleId",sysLogin.getRoleId());
            map.put("role","01".equals(sysLogin.getRoleId())?"县领导":"02".equals(sysLogin.getRoleId())?"主要领导":"03".equals(sysLogin.getRoleId())?"分管领导":
                    "04".equals(sysLogin.getRoleId())?"经办人":"10".equals(sysLogin.getRoleId())?"县主管":"20".equals(sysLogin.getRoleId())?"管理员":"未知");
            map.put("count", loginLogs.size());//登录次数
            mapList.add(map);
        }

        //将maplist根据登录次数重新排序
        if (mapList != null && mapList.size() > 1) {
            Collections.sort(mapList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Integer o1Value = Integer.valueOf(o1.get("count").toString());
                    Integer o2Value = Integer.valueOf(o2.get("count").toString());
                    return o2Value.compareTo(o1Value);
                }
            });
        }
        return mapList;
    }
}
